#include "sqlite_query_interface.h"

std::optional<QSqlQuery> ReadAllFromPersonQuery::Execute(const QSqlDatabase &database)
{
    QSqlQuery query(database);
    query.prepare("select * from person");
    if (!query.exec()) {
        qDebug() << query.lastError();
        return std::nullopt;
    }
    return query;
}

ReadNameFromPersonQuery::ReadNameFromPersonQuery(int id) : id_(id) {}

std::optional<QSqlQuery> ReadNameFromPersonQuery::Execute(const QSqlDatabase &database)
{
    QSqlQuery query(database);
    query.prepare("select name from person where id = :id");
    query.bindValue(":id", id_);
    if (!query.exec()) {
        qDebug() << query.lastError();
        return std::nullopt;
    }
    return query;
}

ReadFromPersonByPageQuery::ReadFromPersonByPageQuery(int page, int number_of_pages)
    : page_(page), number_of_pages_(number_of_pages) {}

std::optional<QSqlQuery> ReadFromPersonByPageQuery::Execute(const QSqlDatabase &database)
{
    QSqlQuery query(database);
    query.prepare("select * from person limit :offset, :count_page");
    query.bindValue(":offset", (page_ - 1) * number_of_pages_);
    query.bindValue(":count_page", number_of_pages_);

    if (!query.exec()) {
        qDebug() << query.lastError();
        return std::nullopt;
    }
    return query;
}
